package testshuju;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/14.
 */
public class ContactEntity {
    private String name;
    private List<String> phoneNums;
    private List<String> emails;
    private String address;

    public ContactEntity(){
        name = address = "";
        phoneNums = new ArrayList<>();
        emails = new ArrayList<>();
    }

    public ContactEntity(String name, String phoneNum) {
        this();
        this.name = name;
        List<String> phoneNums = new ArrayList<>();
        phoneNums.add(phoneNum);
        this.phoneNums = phoneNums;
    }

    public ContactEntity(String name, List<String> phoneNums, List<String> emails, String address) {
        this();
        this.name = name;
        this.phoneNums = phoneNums;
        this.emails = emails;
        this.address = address;
    }

    public static String toVCardFormat(ContactEntity contactEntity){
        StringBuffer sb = new StringBuffer();
        sb.append("BEGIN:VCARD\nVERSION:3.0\n");
        sb.append("FN:"+contactEntity.getName()+"\n");
        for(String tel: contactEntity.getPhoneNums()) sb.append("TEL:"+tel+"\n");
        for(String email: contactEntity.getEmails()) sb.append("EMAIL:"+email+"\n");
        sb.append("ADR:"+contactEntity.getAddress()+"\n");
        sb.append("END:VCARD");
        return sb.toString();
    }

    private static final String VCARD_BEGIN = "BEGIN:VCARD";
    private static final String VCARD_NAME = "FN:";
    private static final String VCARD_TEL = "TEL:";
    private static final String VCARD_EMAIL = "EMAIL:";
    private static final String VCARD_ADR = "ADR:";
    private static final String VCARD_END = "END:VCARD";

    public static ContactEntity parseFromVCard(String vcardStr){
        if(TextUtils.isEmpty(vcardStr)) return null;
        if(!vcardStr.startsWith(VCARD_BEGIN)) return null;
        String[] lines = vcardStr.split("\n");
        if(lines.length < 3) return null;
        ContactEntity contactEntity = new ContactEntity();
        for(int i = 0; i < lines.length; i++){
            if(lines[i].endsWith(":")) continue;
            else if(lines[i].startsWith(VCARD_NAME)) contactEntity.setName(lines[i].substring(VCARD_NAME.length()));
            else if(lines[i].startsWith(VCARD_TEL)) contactEntity.getPhoneNums().add(lines[i].substring(VCARD_TEL.length()));
            else if(lines[i].startsWith(VCARD_EMAIL)) contactEntity.getEmails().add(lines[i].substring(VCARD_EMAIL.length()));
            else if(lines[i].startsWith(VCARD_ADR)) contactEntity.setAddress(lines[i].substring(VCARD_ADR.length()));
            else if(lines[i].startsWith(VCARD_END)) break;
        }
        return contactEntity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getPhoneNums() {
        return phoneNums;
    }

    public void setPhoneNums(List<String> phoneNums) {
        this.phoneNums = phoneNums;
    }

    public List<String> getEmails() {
        return emails;
    }

    public void setEmails(List<String> emails) {
        this.emails = emails;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }



}
